package com.safe.ly_safe.api;

import android.util.Log;

import com.safe.ly_safe.base.BaseApplication;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * @PACKAGE :com.safe.ly_safe.api
 * @Author : HSF
 * @Time : On 2022/4/22 15:54
 * @Description : OkHttpInterceptor
 */
public class OkHttpInterceptor implements Interceptor {

    private static final String TAG = "myOkHttp";

    //设缓存有效期为1天
    //private static final long CACHE_CONTROL_CACHE = 60 * 60 * 24 * 1;

    @Override
    public Response intercept(Chain chain) throws IOException {
        //从chain对象中可以获取到request和response，想要的数据都可以从这里获取
        Request request = chain.request();
//        if (!NetworkUtils.isConnected()){//如果不能上网，那么启用缓存
//            request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
//        }

        //增加头部信息
        //request = addHeaders(request);
//        Request newRequest = request.newBuilder()
//                .addHeader("sessionId",sessionId)
//                .build();
        Request newRequest = request.newBuilder()
                .build();
        //打印request日志
        logForRequest(newRequest);
        //获取response对象
        Response response = chain.proceed(newRequest);
        //打印response日志
        logForResponse(response);
        return response;
//        if(NetworkUtils.isConnected()){
//            //有网的时候读接口上的@Headers里的配置，可以在这里进行统一的设置
//            String cacheControl = request.cacheControl().toString();
//            return response.newBuilder()
//                    .header("Cache-Control", cacheControl)
//                    .removeHeader("Pragma")
//                    .build();
//        }else{
//            return response.newBuilder()
//                    .header("Cache-Control", "public, only-if-cached, max-stale=" + CACHE_CONTROL_CACHE)
//                    .removeHeader("Pragma")
//                    .build();
//        }
    }

    //    private Request addHeaders(Request request) {
//        String sessionId = MyApplication.getInstance().getSessionId();
//        Log.i(TAG,"sessionId>>>>>>>>>>>>>>>>"+sessionId);
//        if(sessionId == null){
//            sessionId = "";
//        }
//        Map<String,String> params=new HashMap<>();
//        params.put("sessionId",sessionId);
//        Headers headers= Headers.of(params);
//
//        Request.Builder requestBuilder = request.newBuilder()
//                //.addHeader("Accept-Encoding", "gzip")
//                .addHeader("Accept", "application/json")
//                .addHeader("Content-Type", "application/json; charset=utf-8")
//                .method(request.method(), request.body());
//        return requestBuilder.headers(headers).build();
//    }

    /**
     * 请求参数的Log
     * @param request
     */
    private void logForRequest(Request request) {
        Log.e(TAG,"Request's Log----------------Start");
        Log.e(TAG,"request_url: "+request.url());
        Log.e(TAG,"request_method: "+request.method());
        Headers headers=request.headers();
        if (headers != null && headers.size() != 0){
            Log.e(TAG,"request_headers: "+headers.toString());
        }
        RequestBody body = request.body();
        if (body != null){
            Log.e(TAG,"request_body"+body.toString());
        }
        Log.e(TAG,"Request's Log>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<End");
    }
    /**
     * 返回数据的Log
     * @param response
     */
    private void logForResponse(Response response) {
        Log.e(TAG,"Response's Log----------------Start");
        Log.e(TAG,"response_code: "+response.code());
        Log.e(TAG,"response_protocol: "+response.protocol());
        Headers headers = response.headers();
        if (headers != null && headers.size() != 0){
            Log.e(TAG,"response_headers:"+headers.toString());
        }
        try {
            //这里不能直接用response.body().string(),因为调用改方法后流就关闭，程序就可能会发生异常
            //我们需要创建出一个新的ResponseBody给应用层调用
            ResponseBody body = response.peekBody(1024*1024);
            Log.e(TAG,"response_protocol: "+body.string());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.e(TAG,"Response's Log>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<End");
    }

}
